package orderoid.soap.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import orderoid.soap.Tool.DeserialisationTool;
import orderoid.soap.classeCommune.Menu;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;
import android.util.Log;

public class webRechercherMenuFromRestau extends AsyncTask<String, Void, List<Menu>> {
	
	private static final String PARAM_IDRESTAURANT = "idRestaurant";
	
	@SuppressWarnings("unchecked")
	@Override
	protected List<Menu> doInBackground(String... params) {
		String idRestaurant = params[0];
		
		List<Menu> listeMenu = new ArrayList<Menu>();

		Log.e("Test", "doInBackground : WebService Recherche Menu");

		SoapObject request = new SoapObject(
				orderoid.soap.constant.method.NAMESPACE,
				orderoid.soap.constant.method.METHOD_NAME_RECHERCHE_MENU);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		
		envelope.setOutputSoapObject(request);
		
		HttpTransportSE androidHttpTransport = new HttpTransportSE(
				orderoid.soap.constant.method.URL);

		request.addProperty(PARAM_IDRESTAURANT, idRestaurant);
		
		try {
			androidHttpTransport.call(orderoid.soap.constant.method.SOAP_ACTION_RECHERCHE_MENU, envelope);
			
			Vector<SoapObject> soapObjectResp = (Vector<SoapObject>) envelope.getResponse();
			
			for ( SoapObject soC : soapObjectResp)
			{
//				if ( soC.hasProperty("idRestaurant") ) 
//				{
					listeMenu.add(DeserialisationTool.deserialisationMenu(soC));
//				}
			}
			
		} catch (Exception e) {
			Log.e("Erreur :",e.getMessage());
			listeMenu = null;
		}
		return listeMenu;
	}

}
